/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog,datepicker ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            //loading();
            this.event();
        },
        event: function() {
            this.data = {};
            var _this = this;
            $("#select_goods").on("click",function(e){
                e.preventDefault();
                _this.select_goods(site_id,e);
                $("#div_content_goods").show();
            });
            $("#btn-search").on("click",function(e){
                e.preventDefault();
                _this.select_goods(site_id,e);
            });
            $("#close_goods").on("click",function(e){
                e.preventDefault();
                $("#div_content_goods").hide();
            });
            //保存
            $('.long_date').on('click', function() {
                WdatePicker({
                    el:this.id,
                    dateFmt:'yyyy-MM-dd HH:mm:ss'
                });
            });
            $("#btn_save").on("click",function(){
                _this.data.type=$("input[name='type']:checked").val();
                if(_this.data.type==2){
                    _this.data.goods_id = $('#goods_id').val();
                    if(_this.data.goods_id==''||_this.data.goods_id==undefined){
                        Alert({msg:'请选择商品',type:'error'});
                        return false;
                    }
                }
                _this.data.start_time =$("#dt_start").val();
                if(_this.data.start_time==''){
                    Alert({msg:'请输入开始日期',type:'error'});
                    return false;
                }
                _this.data.end_time =$("#dt_end").val();
                if(_this.data.end_time==''){
                    Alert({msg:'请输入结束日期',type:'error'});
                    return false;
                }
                _this.data.total_amount= $.trim($("#total_amount").val());
                if(_this.data.total_amount==''){
                    Alert({msg:'请输入投放金额',type:'error'});
                    return false;
                }
                _this.data.amount= $.trim($("#amount").val());
                if(_this.data.amount==''){
                    Alert({msg:'请输入单次分享金额',type:'error'});
                    return false;
                }
                new Model({
                    url: _global.url.api + 'cpa_plan',
                    data:_this.data
                }).post()
                location.href='/site/cpa'
            });
            $('body').on('click','.item-bind',function(e){
                e.preventDefault();
                $('#goods-item').html("");
                var goods_id =$(this).attr('attr');
                var goods_name =$(this).attr('all');
                var price=$(this).attr('price');
                $('#goods-item').append(template('goods-item-tpl',{goods_id:goods_id, goods_name:goods_name,price:price}));
                $("#div_goods").show();
                $('.goods-item-del').on('click', function(e) {
                    e.preventDefault();
                    $(this).parent().parent().remove();

                });
                $(this).addClass('bind_success').removeClass('btn-default');
               // console.log($(this).parent().parent().siblings('tr').find('.item-bind'));
                $(this).parent().parent().siblings().find('.item-bind').removeClass('bind_success').addClass('btn-default');
            });
            $("input[name='type']").on("click",function(){
               if($(this).val()==2){
                   $(".show_goods").show();
               } else{
                   $(".show_goods").hide();
               }
            });
            $("#total_amount").keyup(function(e){
                e.preventDefault();
                var amount = $.trim($(this).val());
                var reg =/^[0-9]+(\.)?([0-9]{1,2})?$/;
                if(amount!=''){
                    if(!reg.test(amount)){
                        Alert({msg:'输入金额有误,请重新输入',type:'error'});
                        $(this).val("");
                        return false;
                    };
                }

            });
            $("#amount").keyup(function(e){
                e.preventDefault();
                var amount = $.trim($(this).val());
                var reg =/^[0-9]+(\.)?([0-9]{1,2})?$/;
                if(amount!=''){
                    if(!reg.test(amount)){
                        Alert({msg:'输入金额有误,请重新输入',type:'error'});
                        $(this).val("");
                        return false;
                    };
                }
            });
        },
        select_goods:function(site_id,e){
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-items',
                url: _global.url.api + 'promotion_goods_search',
                condition: {site_id: site_id,keyword:$("#keyword").val(),limit:5},
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        }

    };




    new index();

});